package com.lyhengtongwl.zqsnews.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.hubert.guide.NewbieGuide;
import com.app.hubert.guide.core.Controller;
import com.app.hubert.guide.listener.OnHighlightDrewListener;
import com.app.hubert.guide.listener.OnLayoutInflatedListener;
import com.app.hubert.guide.model.GuidePage;
import com.app.hubert.guide.model.HighLight;
import com.app.hubert.guide.model.HighlightOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.NewsEntity;
import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;
import com.lyhengtongwl.zqsnews.utils.FTYUtils;
import com.lyhengtongwl.zqsnews.utils.ImgHelper;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

public class NewsAdapter extends BaseQuickAdapter<NewsEntity, BaseViewHolder> {
    private Activity activity;
    private String newsType;
    private ImageView imageView;

    public NewsAdapter(Activity activity, List<NewsEntity> newsList, String newsType) {
        super(R.layout.item_news, newsList);
        this.activity = activity;
        this.newsType = newsType;
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, NewsEntity item) {
        String title = item.getTitle();
        String linkUrl = item.getUrl();
        String money = item.getMoney();
        TextView tt = ((TextView) helper.getView(R.id.tv_title));
        TextView tm = ((TextView) helper.getView(R.id.tv_money));
        TextView tf = ((TextView) helper.getView(R.id.tv_forward));
        TextView tl = ((TextView) helper.getView(R.id.tv_gg));
        if (!"17".equals(item.getTypeId())) {//不是广告的
            tf.setVisibility(View.VISIBLE);
            tt.setText(title);
            tm.setText("￥" + FTYUtils.fenToYuan(money).split("\\.")[0]);
            tl.setText("累计收益");
            tf.setText("转发赚钱");
//            (helper.getView(R.id.tv_forward)).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    activity.startActivity(new Intent(activity, NewsWebDetailActivity.class).putExtra("url", item.getContentUrl()));
//                }
//            });
            Picasso.with(App.getContext()).load(linkUrl).placeholder(R.mipmap.error).error(R.mipmap.error).into((RoundedImageView) helper.getView(R.id.iv_preview));
        } else {//表示是广告
            tf.setVisibility(View.GONE);
            tl.setText("广告");
            tt.setText(title);
            Picasso.with(App.getContext()).load(linkUrl).placeholder(R.mipmap.error).error(R.mipmap.error).into((RoundedImageView) helper.getView(R.id.iv_preview));
        }
        //设置新手指引
        if ("1".equals(newsType) && helper.getPosition() == 0) {
            setNewsGuide(helper.getView(R.id.tv_forward));
        }
    }

    //设置新手指引
    private void setNewsGuide(View view1) {
        HighlightOptions options = new HighlightOptions.Builder()
                .setOnHighlightDrewListener(new OnHighlightDrewListener() {
                    @SuppressLint("NewApi")
                    @Override
                    public void onHighlightDrew(Canvas canvas, RectF rectF) {
                        Paint paint = new Paint();
                        paint.setColor(Color.WHITE);
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(2);
                        paint.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));
                        Matrix m = new Matrix();
                        m.setRotate(0);
                        m.setTranslate(0, 0);
                        Bitmap bitmapFormResources = ImgHelper.getBitmapFormResources(activity, R.drawable.img_frame);
                        float imgX = (bitmapFormResources.getWidth() - rectF.width()) / 2;
                        float imgY = (bitmapFormResources.getHeight() - rectF.height()) / 2;
                        float hightLightX = rectF.centerX() - rectF.width() / 2 - imgX;
                        float hightLightY = rectF.centerY() - rectF.height() / 2 - imgY;
                        canvas.drawBitmap(bitmapFormResources, hightLightX, hightLightY, paint);

                        Bitmap bitmapFormResources1 = ImgHelper.getBitmapFormResources(activity, R.drawable.img_text);
                        float hightLightX1 = rectF.centerX() - imgX * 2 - rectF.width() / 2;
                        float hightLightY1 = rectF.centerY() + rectF.height() / 2;
                        canvas.drawBitmap(bitmapFormResources1, hightLightX1, hightLightY1, paint);
                        setImageView(rectF.centerY() + bitmapFormResources1.getHeight() + rectF.height() / 2 + bitmapFormResources.getHeight());
                    }
                })
                .build();
        NewbieGuide.with(activity)
                .setLabel("guide")
                .addGuidePage(GuidePage.newInstance()
                        .addHighLightWithOptions(view1, HighLight.Shape.ROUND_RECTANGLE, ScreenUtil.dip2px(activity, 12), 0, options)
                        .setLayoutRes(R.layout.forward_newsguide)
                        .setOnLayoutInflatedListener(new OnLayoutInflatedListener() {
                            @Override
                            public void onLayoutInflated(View view, Controller controller) {
                                TextView tv = view.findViewById(R.id.tv_tiaoguo);
                                imageView = view.findViewById(R.id.iv_next);
                                imageView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        controller.remove();
                                        ((MainActivity) activity).getTabLayout(1).setCurrentTab(1);
                                    }
                                });
                                tv.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        //保存状态
                                        SPUtils.put(App.getContext(), "isCloseGuide", "true");
                                        controller.remove();
                                    }
                                });
                            }
                        })
                        .setEverywhereCancelable(false)
                )
                .show();
    }

    private void setImageView(float top) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        //设置控件的宽高
        //设置各个方向上的间距
        params.setMargins(0, (int) top, 0, 0);
        //改变控件的属性
        imageView.setLayoutParams(params);
    }
}
